﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace IRCTest
{
    /*
     * The absolute base for an event
     */
    public class IRCBasicEvent : EventArgs
    {

        protected String line;
        protected String channel;
        protected String user;


        public String Line { get { return this.line; } }
        public String Channel { get { return this.channel; } }
        public String User { get { return this.user; } }

        public IRCBasicEvent(String Line)
        {
            this.channel = IRCActions.getReceipient(Line);
            this.user = IRCActions.getUser(Line);
            this.line = Line;

        }

    }
    /*
     * If someone said something towards the channel.
     */
    public class IRCMessageEvent : IRCBasicEvent
    {
        private String message;
        private String command;

        public String Message { get { return this.message; }  }
        public String Command { get{ return this.command; } }

        public IRCMessageEvent(String Line) : base (Line)
        {
            this.message = IRCCommands.getMessage(Line);
            this.command = IRCCommands.getCommand(Line);
              
        }

    }
    /*
     * If a user has joined/left or changed nick
     */
    public class IRCUserEvent : IRCBasicEvent
    {
        private bool joined = false;
        public bool Joined { get { return this.joined; } }

        private bool nickChange = false;
        public bool NickChange { get { return this.nickChange; } }

        private String newNick = String.Empty;
        public String NewNick { get { return this.newNick; } }
        public IRCUserEvent(String Line) : base(Line)
        {            
            this.joined = IRCCommands.getCommand(Line).Equals("JOIN");
            this.nickChange = IRCCommands.getCommand(Line,true).Equals("NICK");

            if (this.nickChange)
            {
                this.newNick = IRCActions.getNewNick(Line);
            }
        }
        
    }
    /*
     * If someone has requested to execute a comman
     */
    public class IRCCommandEvent : IRCBasicEvent
    {
        protected String command;
        public String CommandName { get { return this.command; } }

        protected String[] parameters = new String[]{};
        public String[] Parameters { get { return this.parameters;} }

        public IRCCommandEvent(String Line) : base(Line)
        {
            Regex CommandPat = new Regex(@"(?<=^!)[A-z]+");
            if (CommandPat.IsMatch(IRCActions.getMessage(Line)))
            {
                this.command = CommandPat.Match(IRCActions.getMessage(Line)).Value;
            }
            getParameters(Line);
            
        }

        private void getParameters(String Line){
            List<String> Parameters = new List<String>();
            Regex ParameterPat = new Regex("(?<![!A-z#@\\-]+)[A-z0-9\\-+|:²³&@#\"'\\^èç\\{\\}°)*$%´`~+<>]+");
            String Message = IRCActions.getMessage(Line).Substring(0,command.Length+1);
            foreach (Match Parameter in ParameterPat.Matches(IRCActions.getMessage(Line)))
                Parameters.Add(Parameter.Value);
            parameters = Parameters.ToArray();
        }

    }
    /*
     * If the Userlist is updated.
     */
    public class IRCUserList : IRCBasicEvent
    {
        private String code;
        private List<String> users = new List<String>();
        public List<String> Users { get { return this.users; } }
        public String Code { get { return this.code; } }
       
        public IRCUserList(String Line) : base(Line)
        {
            IRCActions.getCode(Line,out this.code);
            if (this.code.Equals("353")) 
            try{
                StringBuilder x = new StringBuilder();
                String d = IRCActions.getMessage(Line);
                foreach (char c in d)
                {
                    if (!(c.Equals('#') || c.Equals('@') || c.Equals('+')))
                        x.Append(c);
                }
                var e = from user in x.ToString().Trim().Split(' ').AsEnumerable() select user;
                Users.AddRange(e);
            }
            catch(Exception exc){
            }
                
        }
    }
    /*
     * Upon a private message towards the bot
     */
    public class IRCPrivMsg : IRCMessageEvent
    {
        public IRCPrivMsg(String line)
            : base(line)
        {

        }
    }
    /*
     * If someone was kicked
     */
    public class IRCKick : IRCBasicEvent
    {
        private String kickedbyUser;
        public String KickedbyUser { get { return this.kickedbyUser; } }
        private String reason;
        public String Reason { get { return this.reason; } }
        public IRCKick(String Line) : base (Line)
        {
            getTarget();
        }

        private void getTarget()
        {
            Regex Target = new Regex(@"(?<=^:[A-z\.!@\-0-9\s]+KICK\s[#A-z0-9]+\s)[A-z0-9]+");
            Regex Reason = new Regex(@"(?<=^:[A-z\.!@\-0-9\s]+KICK\s[#A-z0-9]+\s[A-z0-9]+\s:?)[\.]+");

            this.user = Target.Match(this.line).Value.Trim();
            this.kickedbyUser = IRCActions.getUser(this.line);
            this.reason = Reason.Match(this.line).Value.Trim();
        }
    }
}
